>>> import re >>> re.sub('\r?\n', ' $ ', 'a\r\nb\r\nc') 'a $ b $ c' >>> re.sub('\r?\n', ' $ ', 'a\nb\nc') 'a $ b $ c'